#include "qchosseimage.h"

#include "../../include/abstractcore.h"
#include "../../include/resourceinterface.h"
#include "../../lib/shared/qproject.h"
#include "../../lib/corelib/qcommondelegate.h"

#include <QVBoxLayout>
#include <QPixmap>

ImageResourceDialog::ImageResourceDialog(AbstractCore *core, QWidget *parent):
    QDialog(parent),
    m_tree(new QCommonTreeWidget(this)),
    m_core(core)
{
    m_tree->setItemDelegate(new QCommonDelegate());
    m_tree->setAlternatingRowColors(true);
    this->resize(200,300);

    QVBoxLayout *l=new QVBoxLayout;
    l->setMargin(0);
    l->setSpacing(0);
    l->addWidget(m_tree);

    this->setLayout(l);

    m_tree->setFrameStyle(QFrame::NoFrame);

    m_tree->setColumnCount(1);
    m_tree->setHeaderLabel("Image Name");

    init();
}

void ImageResourceDialog::init()
{
    QResourceInterface *res=m_core->resourceInterface();

    QStringList tops=res->getTopLevelNames();
    QStringList fs;

    QTreeWidgetItem *root;
    QTreeWidgetItem *item;

    foreach(QString t,tops)
    {
        root=new QTreeWidgetItem(m_tree);
        root->setText(0,t);
        root->setToolTip(0,t);
        root->setIcon(0,QIcon(":/images/resource_toplevel.png"));
        m_roots.append(root);
        fs=res->getResourceNames(t);
        foreach(QString f,fs)
        {
            item=new QTreeWidgetItem(root);
            item->setText(0,f);
            item->setToolTip(0,f);
            QPixmap p;
            p.loadFromData(res->getResourceData(t,f));
            item->setIcon(0,QIcon(p));
        }
    }
    connect(m_tree,SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),this,SLOT(itemDoubleClicked(QTreeWidgetItem*,int)));
}

void ImageResourceDialog::itemDoubleClicked(QTreeWidgetItem *item, int index)
{

    if(item->childCount()!=0)
    {
        return;
    }
    QTreeWidgetItem *p=item->parent();

    m_select=p->text(0)+"/"+item->text(0);
    close();

}

QString ImageResourceDialog::getSelect()
{
    return m_select;
}


QChosseImage::QChosseImage(AbstractCore *core, QWidget *parent):
    QDialog(parent),
    m_imageWidget(new QWidget(this)),
    m_imageButton(new QPushButton("Image",this)),
    m_core(core),
    m_typeCombo(new QComboBox(this)),
    m_okBtn(new QPushButton("Ok",this)),
    m_cancleBtn(new QPushButton("Cancel",this)),
    m_clearBtn(new QPushButton("Clear",this)),
    m_ret(0)
{
    this->setFixedSize(500,300);
    m_imageWidget->setGeometry(10,10,280,280);


    m_imageButton->setGeometry(310,20,180,20);
    m_typeCombo->setGeometry(310,50,180,20);

    connect(m_imageButton,SIGNAL(clicked()),this,SLOT(showImage()));
    m_typeCombo->addItems(QStringList()<<"Image"<<"Border Image"<<"Background Image");
    m_typeCombo->setCurrentIndex(1);

    for(int i=0;i<4;i++)
    {
        m_label[i]=new QLabel(this);
        m_border[i]=new QSpinBox(this);
        m_border[i]->setRange(0,65535);
        m_border[i]->setValue(0);
        m_label[i]->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
        m_label[i]->setGeometry(310,80+30*i,60,20);
        m_border[i]->setGeometry(375,80+30*i,115,20);
        connect(m_border[i],SIGNAL(valueChanged(int)),this,SLOT(updateStyleSheet()));
    }

    m_okBtn->setGeometry(310,200,180,20);
    m_cancleBtn->setGeometry(310,230,180,20);
    m_clearBtn->setGeometry(310,260,180,20);

    m_label[0]->setText("Left:");
    m_label[1]->setText("Top:");
    m_label[2]->setText("Bottom:");
    m_label[3]->setText("Right:");
    updateStyleSheet();

    connect(m_typeCombo,SIGNAL(currentIndexChanged(int)),this,SLOT(typeChanged(int)));

    connect(m_okBtn,SIGNAL(clicked()),this,SLOT(clicked()));
    connect(m_cancleBtn,SIGNAL(clicked()),this,SLOT(cancel()));
    connect(m_clearBtn,SIGNAL(clicked()),this,SLOT(clear()));
}

void QChosseImage::updateStyleSheet()
{
    QString str="background-color:rgb(255,255,255);";
    if(m_select!=0)
    {
        if(m_typeCombo->currentIndex()==0)
        {
            str+=QString("image:url(:/soft_res/%1);").arg(m_select);
        }
        else if(m_typeCombo->currentIndex()==1)
        {
            str+=QString("border-width:%1 %2 %3 %4;").arg(m_border[1]->value()).arg(m_border[3]->value()).arg(m_border[2]->value())
                    .arg(m_border[0]->value());
            str+=QString("border-image:url(:/soft_res/%1)%2 %3 %4 %5;").arg(m_select).arg(m_border[1]->value())
                    .arg(m_border[3]->value()).arg(m_border[2]->value()).arg(m_border[0]->value());
        }
        else if(m_typeCombo->currentIndex()==2)
        {
            str+=QString("background-image:url(:/soft_res/%1);").arg(m_select);
        }
    }
    m_imageWidget->setStyleSheet(str);
}

void QChosseImage::typeChanged(int index)
{
    if(index==1)
    {
        for(int i=0;i<4;i++)
        {
            m_border[i]->setEnabled(true);
        }
    }
    else
    {
        for(int i=0;i<4;i++)
        {
            m_border[i]->setEnabled(false);
        }
    }

    updateStyleSheet();
    if(m_enabledList.indexOf(QString::number(m_typeCombo->currentIndex()))>=0)
    {
        m_okBtn->setEnabled(false);
    }
    else
    {
        m_okBtn->setEnabled(true);
    }
}

void QChosseImage::showImage()
{
    ImageResourceDialog dlg(m_core,this);

    dlg.exec();

    QString str=dlg.getSelect();

    if(str!="")
    {
        if(str==m_select)
        {
           return;
        }
        if(m_select!="")
        {
            QProject::project()->removeTempResource(m_select);
        }
        m_select=str;
        QProject::project()->inserTempResource(m_select);
        for(int i=0;i<4;i++)
        {
            m_border[i]->setValue(0);
        }
        updateStyleSheet();
    }
}

void QChosseImage::clicked()
{
    m_ret=1;
    close();
}

int QChosseImage::exec()
{
    QDialog::exec();
    if(m_select!="")
    {
        QProject::project()->inserTempResource(m_select);
    }
    return m_ret;
}

QString QChosseImage::getResource()
{
    return m_select;
}

int QChosseImage::getType()
{
    return m_typeCombo->currentIndex();
}

QRect QChosseImage::getBorder()
{
    QRect re(m_border[0]->value(),m_border[1]->value(),m_border[3]->value(),m_border[2]->value());
    return re;
}

void QChosseImage::init(const QString &name, const QRect &re, int type)
{
    m_select=name;
    m_border[0]->setValue(re.left());
    m_border[1]->setValue(re.top());
    m_border[2]->setValue(re.width());
    m_border[3]->setValue(re.height());
    m_typeCombo->setCurrentIndex(type);
    if(m_select!="")
    {
        QProject::project()->inserTempResource(m_select);
    }
    updateStyleSheet();
}

void QChosseImage::cancel()
{
    m_ret=0;
    close();
}

void QChosseImage::clear()
{
    m_select="";
    for(int i=0;i<4;i++)
    {
        m_border[i]->setValue(0);
    }
    if(m_select!="")
    {
        QProject::project()->inserTempResource(m_select);
    }
    updateStyleSheet();
}

void QChosseImage::setEnabledList(QStringList list)
{
    m_enabledList=list;
    if(m_enabledList.indexOf(QString::number(m_typeCombo->currentIndex()))>=0)
    {
        m_okBtn->setEnabled(false);
        QStringList l=QStringList()<<"0"<<"1"<<"2";
        foreach(QString s,list)
        {
            l.removeAll(s);
        }
        if(l.size()>0)
        {
            m_typeCombo->setCurrentIndex(l.first().toInt());
        }
    }
}
